package com.wxy.gks.springbootprojectday22practice.web.controller;

import com.wxy.gks.springbootprojectday22practice.bean.Dept;
import com.wxy.gks.springbootprojectday22practice.bean.Employee;
import com.wxy.gks.springbootprojectday22practice.service.DeptService;
import com.wxy.gks.springbootprojectday22practice.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private DeptService deptService;
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/findAll")
    public String findEmployees(HttpServletRequest request){
        List<Employee> employees = employeeService.findEmployees();
//        map.put("employees",employees);
            request.setAttribute("employees",employees);

        return "list";




    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){

        employeeService.delete(id);
        redisTemplate.delete("employees");
        return "redirect:/employee/findAll";


    }
    @RequestMapping("/toUpdate/{id}")
    public String getEmployee(@PathVariable("id") Integer id, Model model){
        Employee employee = employeeService.getEmpById(id);
        List<Dept> depts = deptService.findAll();

        model.addAttribute("emp",employee);
        model.addAttribute("depts",depts);
        System.out.println("=============");
        System.out.println(employee);
        return "update";


    }
    @RequestMapping("/modify")
    public String modify(Employee employee){

        employeeService.update(employee);


        return "redirect:findAll";
    }
    @RequestMapping("/toAdd")
    public String toAdd(Model model){
        List<Dept> depts = deptService.findAll();
        model.addAttribute("depts",depts);
        return "add";
    }
    @RequestMapping("/add")
    public String add(Employee employee){
        double id = Math.random() * 9000 + 1000;
        int id2= (int) id;
        employee.setId(id2);
        employeeService.add(employee);



        return "redirect:findAll";

    }


    @RequestMapping("/deletes/{ids}")
    public  String deletes(@PathVariable("ids") String ids){
        String[] idArray = ids.split(",");
        System.out.println("=========================++++++++++++");
        System.out.println(idArray);
        for (int id = 0; id < idArray.length; id++) {

            employeeService.delete(new Integer(idArray[id]));

        }
        redisTemplate.delete("employees");
        System.out.println("这是需要删除的数据");
        return "redirect:/employee/findAll";


    }


}
